package com.dc.business.datacollectbusiness.utils;

import com.alibaba.fastjson.JSONObject;
import com.dc.business.datacollectbusiness.entity.Collector;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

@Component
public class HttpPostToRealCollectorUtil {
    @Autowired
    private static RestTemplate restTemplate;
    private static  String ip="http://81.70.218.196:11025/";
//    private static  String ip="http:///";
    public static String postToRealCollector(Collector collector) {
        LinkedHashMap<String, Object> jsonObject1 = new LinkedHashMap<>();
        LinkedHashMap<String, Object> jsonObject2 = new LinkedHashMap<>();
        LinkedHashMap<String, Object> jsonObject3 = new LinkedHashMap<>();
        LinkedHashMap<String, Object> jsonObject4 = new LinkedHashMap<>();
        LinkedHashMap<String, Object> jsonObject5 = new LinkedHashMap<>();

        // 构建JSON对象
        LinkedHashMap<String, Object> policy = new LinkedHashMap<>();
        policy.put("CMD", "SP");
        policy.put("PV", collector.getPvVer());
        policy.put("ID", collector.getCollectNumber());
        policy.put("IP1", collector.getPlatformInterface());
        policy.put("OM", collector.getWorkMode());
        policy.put("WM", collector.getAwakeMode());
        policy.put("AC", collector.getCollectInterval());
        if ("0".equals(collector.getWorkMode()) || "1".equals(collector.getWorkMode())) {
            jsonObject1.put("CH",collector.getCh1());
            jsonObject1.put("DI",collector.getDi1());

            jsonObject2.put("CH",collector.getCh2());
            jsonObject2.put("DI",collector.getDi2());

            jsonObject3.put("CH",collector.getCh3());
            jsonObject3.put("DI",collector.getDi3());

            jsonObject4.put("CH",collector.getCh4());
            jsonObject4.put("DI",collector.getDi4());

            jsonObject5.put("CH",collector.getCh5());
            jsonObject5.put("DI",collector.getDi5());

            LinkedHashMap<String, Object> [] seArray=new LinkedHashMap[5];
            seArray[0]=jsonObject1;
            seArray[1]=jsonObject2;
            seArray[2]=jsonObject3;
            seArray[3]=jsonObject4;
            seArray[4]=jsonObject5;

            policy.put("SE", filterEmptyCH(seArray));
        } else if ("2".equals(collector.getWorkMode())) {
            jsonObject1.put("CH",collector.getDh1());
            jsonObject1.put("ST",collector.getSt1());
            jsonObject1.put("BR",collector.getBr1());
            jsonObject1.put("AD",collector.getAd1());
            jsonObject1.put("DT",collector.getDt1());

            jsonObject2.put("CH",collector.getDh2());
            jsonObject2.put("ST",collector.getSt2());
            jsonObject2.put("BR",collector.getBr2());
            jsonObject2.put("AD",collector.getAd2());
            jsonObject2.put("DT",collector.getDt2());

            jsonObject3.put("CH",collector.getDh3());
            jsonObject3.put("ST",collector.getSt3());
            jsonObject3.put("BR",collector.getBr3());
            jsonObject3.put("AD",collector.getAd3());
            jsonObject3.put("DT",collector.getDt3());

            jsonObject4.put("CH",collector.getDh4());
            jsonObject4.put("ST",collector.getSt4());
            jsonObject4.put("BR",collector.getBr4());
            jsonObject4.put("AD",collector.getAd4());
            jsonObject4.put("DT",collector.getDt4());

            jsonObject5.put("CH",collector.getDh5());
            jsonObject5.put("ST",collector.getSt5());
            jsonObject5.put("BR",collector.getBr5());
            jsonObject5.put("AD",collector.getAd5());
            jsonObject5.put("DT",collector.getDt5());

            LinkedHashMap<String, Object> [] seArray=new LinkedHashMap[5];
            seArray[0]=jsonObject1;
            seArray[1]=jsonObject2;
            seArray[2]=jsonObject3;
            seArray[3]=jsonObject4;
            seArray[4]=jsonObject5;
            policy.put("SE", filterEmptyCH(seArray));
        }
        JSONObject  outerJson = new JSONObject();
        outerJson.put("tf_collecter_product_code", collector.getCollectNumber());
        outerJson.put("policy", policy);

        System.out.println(outerJson.toString());

        MultiValueMap<String, String> httpHeaders = null;
        HttpEntity<String> parameterEntity = new HttpEntity<>(outerJson.toString(), httpHeaders);

        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.postForObject(ip + "api/SetCustomPolicy/", parameterEntity, String.class);

        return response;
    }
    private static  List filterEmptyCH( LinkedHashMap<String, Object>[]  jsonArray) {
        List< LinkedHashMap<String, Object> > filteredList = new ArrayList<>();
        for ( LinkedHashMap<String, Object>  jsonObject : jsonArray) {
            if (jsonObject.get("CH")!=null) {
                filteredList.add(jsonObject);
            }
        }
        return filteredList;
    }
}